<!-- 数据库表SQL管理抽屉界面 -->
<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="displayModuleTitle" width="80%">
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="false"
      :loading="false"
      :data-source="tableData"
    >
      <template #toolbar>
        <a-button type="success" size="small" @click="onAddButtonClick()"
          ><PlusOutlined /> 新增
        </a-button>
      </template>

      <!-- 操作类型 -->
      <template #optypeRender="{ record }">
        <template v-if="record.optype === '0' || record.optype === 0">
          <Tag :color="'blue'">查询</Tag>
        </template>
        <template v-if="record.optype === '1' || record.optype === 1">
          <Tag :color="'green'">新增</Tag>
        </template>
        <template v-if="record.optype === '2' || record.optype === 2">
          <Tag :color="'cyan'">修改</Tag>
        </template>
        <template v-if="record.optype === '3' || record.optype === 3">
          <Tag :color="'red'">删除</Tag>
        </template>
      </template>

      <!-- 标记 -->
      <template #flagRender="{ record }">
        <template v-if="record.flag === '0' || record.flag === 0">
          <Tag :color="'green'">启用</Tag>
        </template>
        <template v-if="record.flag === '5' || record.flag === 5">
          <Tag :color="'yellow'">禁用</Tag>
        </template>
      </template>

      <!-- 可编辑操作标记：查询的数据是否可以进行录入、修改、删除等操作 -->
      <template #editflagRender="{ record }">
        <template v-if="(record.editflag & 0x1) === 0x1">
          <Tag :color="'green'"><PlusOutlined /></Tag>
        </template>
        <template v-if="(record.editflag & 0x2) === 0x2">
          <Tag :color="'blue'"><EditOutlined /></Tag>
        </template>
        <template v-if="(record.editflag & 0x4) === 0x4">
          <Tag :color="'red'"><DeleteOutlined /></Tag>
        </template>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditDBTbleSQLFormModal @register="registerEditSQLFormModal" @success="getDBTableSQL" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons-vue';

  import { dbTableSQLColumns } from './dbtablesql.data';
  import { queryDBTableSQL, deleteDBTableSQL } from '/@/api/platform/dbTableSQL';
  import EditDBTbleSQLFormModal from './EditDBTableSQLModal.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useModal } from '/@/components/Modal';
  import { Tag } from 'ant-design-vue';

  export default defineComponent({
    name: 'DBTableSQLMenagement',
    components: {
      BasicDrawer,
      BasicTable,
      TableAction,
      PlusOutlined,
      EditOutlined,
      DeleteOutlined,
      EditDBTbleSQLFormModal,
      Tag,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      /**
       * 组件之间传递数据的统一属性，包含模块名称、操作类型、数据ID
       */
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();

      /**
       * 显示的模块标题，用于显示在弹出框中的标题
       */
      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });

      const [registerEditSQLFormModal, { openModal: openEditSQLFormModal }] = useModal();

      const { createMessage } = useMessage();

      // sql表数据
      const tableData = ref([]);

      const [registerDrawer] = useDrawerInner((data) => {
        //接收父组件传递的数据
        if (data != undefined && data != null) {
          qcUnifyProps.value = unref(data);
          console.log('接收的对象', qcUnifyProps.value);
        }
        getDBTableSQL();
      });

      const [registerTable] = useTable({
        title: '数据库表SQL管理',
        columns: dbTableSQLColumns,
        showTableSetting: true,
        bordered: true,
        size: 'small',
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      /**
       * 获取指定数据库表编码所有的SQL语句
       */
      function getDBTableSQL() {
        tableData.value = [];
        if (!qcUnifyProps.value?.dataId) {
          // 如果数据库表无效，不做处理
        } else {
          queryDBTableSQL(qcUnifyProps.value?.dataId)
            .then((res) => {
              console.log('res', res);
              if (res != undefined && res != null && res.length > 0x0) {
                tableData.value = res;
              }
            })
            .catch((ex) => {
              console.log('catch ', ex);
              createMessage.error(ex.msg);
            });
        }
      }

      /**
       * 新增按钮事件，打开新增表单
       */
      function onAddButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: 'SQL管理',
          operateType: OperationTypeEnum.ADD,
          dataId: qcUnifyProps.value?.dataId, //传递数据库表编码
        };
        openEditSQLFormModal(true, propsData);
      }

      /**
       * 编辑按钮事件，打开编辑表单
       */
      function onEditButtonClick(record: Recordable) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: 'SQL管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: { tableCode: record.tbcode, code: record.code },
        };
        openEditSQLFormModal(true, propsData);
      }

      /**
       * 删除按钮事件
       */
      function onDeleteButtonClick(record: Recordable) {
        deleteDBTableSQL(record.tbcode, record.code)
          .then((res) => {
            // console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              getDBTableSQL();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      return {
        registerDrawer,
        registerTable,
        displayModuleTitle,
        getDBTableSQL,
        tableData,
        onAddButtonClick,
        onEditButtonClick,
        onDeleteButtonClick,
        registerEditSQLFormModal,
      };
    },
  });
</script>
